# -*- coding:utf-8 -*-
# @Time: 2020-08-29 18:09
# @Author: aniu
# @File: auth.py

'''
    拦截器,对用户登录进行判断。
'''
import json

from flask import request

from application import app


@app.before_request
def before_request():
    token = request.args.get('token')
    userinfo = accessToken(token)
    # 添加全局变量
    g.current_user = userinfo
    # 忽略login
    if request.path == '/login':
        cookies = request.cookies.get('data')
        print('=================',  cookies)
        return
    return



@app.after_request
def after_request( response ):
    return response



def accessToken(token):
    '''
        解析token验证,获取到用户的userid
    '''
    result = parse_payload(token)
    parse_token = json.loads(result.data.decode('utf-8'))
    if parse_token['status'] == 200:
        userid = parse_token['data']['userinfo']
        return userid
    return parse_token['msg']